import Config from '../config/index';
import { Toast } from 'native-base'; 
import AsyncStorage from './AsyncStorage';

// 图片上传
export default async (imgUrl,imgName) => {
	// Token
	let Token = await AsyncStorage.getItem("token");
	
	try {
		let formData = new FormData();// 把图片放入formData中,采用formData来实现
		let file = { uri:  imgUrl, type: 'multipart/form-data', name: imgName };// 这里的key(uri和type和name)不能改变,此处type也有可能是'application/octet-stream',看后台配置
		formData.append('file', file); // 有可能是file 也有可能是images 看后台的配置

		let response = await fetch(
			Config.API_PATH + "/system/upload_file", 
			{
				method: 'POST',
				headers: {
					'Content-Type':'multipart/form-data',
					'token': Token
				},
				body: formData
			});
		
		let responseJson = await response.json();
		return responseJson;
	} catch (error) {
		console.error(error);
	}
}

